package com.ruoyi.app.controller;

import com.ruoyi.app.service.LoginService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 微信登录
     */
    @GetMapping("/getSessionKey")
    public AjaxResult getSessionKey(String code) throws Exception {
        if(StringUtils.isEmpty(code)){
            return AjaxResult.error("code为空");
        }
        return loginService.getSessionKey(code);
    }

    /**
     * 微信登录
     */
    @GetMapping("/getPhone")
    public AjaxResult getPhone(String code, String userId) throws Exception {
        if(StringUtils.isEmpty(code)){
            return AjaxResult.error("code为空");
        }
        if(StringUtils.isEmpty(userId)){
            return AjaxResult.error("userId为空");
        }
        return loginService.getPhone(code,userId);
    }

    /**
     * 微信登录
     */
    @GetMapping("/loginByWechat")
    public AjaxResult loginByWechat(String encryptedData, String sessionKey, String iv, String openId, String unionId) throws Exception {
        if(StringUtils.isEmpty(encryptedData)||StringUtils.isEmpty(sessionKey)||StringUtils.isEmpty(iv)||StringUtils.isEmpty(openId)||StringUtils.isEmpty(unionId)){
            return AjaxResult.error("缺少参数,请检查！");
        }
        return loginService.loginByWechat(encryptedData,sessionKey,iv,openId,unionId);
    }
}
